package model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.LinkedList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import data.Player;


@Path("/player")
public class PlayerService {
	
	public static final String PATH = "../data/DATA_player";
	public static final String PATH_TMP = "../data/DATA_player_tmp";
	
	@GET()
	@Path("/")
	@Produces({MediaType.APPLICATION_XML})
	public LinkedList<Player> getList() {
		LinkedList<Player> ret = new LinkedList<Player>();
		Player pl = null;
		try {
			BufferedReader fichier = new BufferedReader(new FileReader(PlayerService.PATH));
			String chaine = "";
			while ((chaine = fichier.readLine()) != null && !chaine.isEmpty()) {
				String[] p = chaine.split(",");
				pl = new Player(Integer.parseInt(p[0]), p[1], Integer.parseInt(p[2]));
				ret.add(pl);
			}
			fichier.close();
		} catch (FileNotFoundException e) {
			
		} catch (IOException e) {
			
		}
		return ret;
	}
	
	@GET()
	@Path("/{name}")
	@Produces({MediaType.APPLICATION_XML})
	public Player getByName(@PathParam("name") String name) throws RemoteException {
		Player r = new Player();
		try {
			BufferedReader fichier_source = new BufferedReader(new FileReader(PlayerService.PATH));
			String chaine;
			while((chaine = fichier_source.readLine()) != null && r.getId() == null){
				String[] tabChaine = chaine.split(",");
				if (tabChaine[1].equalsIgnoreCase(name)) {
					r = new Player(Integer.valueOf(tabChaine[0]), tabChaine[1], Integer.valueOf(tabChaine[2]));
				}
			}
			fichier_source.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}
	
	@GET
	@Path("/add/{name}")
	@Produces({MediaType.APPLICATION_XML})
	public Player create(@PathParam("name") String name) throws RemoteException {
		Player p = new Player();
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(PlayerService.PATH, true)));
			p.setId(this.getList().size()+1).setLogin(name).setPoints(0).setConnected(false);
			out.println(p.getId()+","+p.getLogin()+",0,0");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}
	
	public static Player getById(Integer id) {
		Player r = null;
		
		try {
			BufferedReader fichier = new BufferedReader(new FileReader(PlayerService.PATH));
			String chaine;
			while((chaine = fichier.readLine()) != null && !chaine.isEmpty() && r == null){
				String[] tabChaine = chaine.split(",");
				if (Integer.parseInt(tabChaine[0]) == id) {
					r = new Player(Integer.valueOf(tabChaine[0]), tabChaine[1], Integer.valueOf(tabChaine[2]));
				}
			}
			fichier.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}
	
}
